import os
import random
from math import sqrt

# game constants
from constants import *

# cocos componets used by this scene
from cocos.director import director
from cocos.scene import Scene
from cocos.layer import Layer, ColorLayer
from cocos.scenes.transitions import *
from cocos.actions import *
from cocos.sprite import Sprite
from cocos.text import Label
from cocos.cocosnode import CocosNode
from cocos.batch import BatchNode
from cocos.draw import Line

# pyglet  componets used by this scene
import pyglet
from pyglet import gl, font
from pyglet.window import key
from pyglet.gl import *

import kytten


class BuildDialog(Layer):
    is_event_handler = True     #: enable pyglet's events
    
    def __init__(self, planet, pickedArea):
        super(BuildDialog, self).__init__()
        
        self.interface = planet.interface
        
        self.population = 0
        self.organics = 0
        self.minerals = 0 
        self.soldierNumber = 0
        
        # add a semi transparent bg
        self.add(ColorLayer(0,0,0,120),z=-2)
        
        director.window.register_event_type('on_update')        
        def update(dt):
            director.window.dispatch_event('on_update', dt)         
            
        self.schedule(update)
        

        self.batchnode = BatchNode()
        self.add(self.batchnode)

        
        self.dialog = kytten.Dialog(
	            kytten.Frame(
	                kytten.VerticalLayout([
		                kytten.Label("Build"),
                        kytten.HorizontalLayout([
                            None,
                            kytten.VerticalLayout([
                                kytten.Label("Build"),
                                kytten.Label("house"),                                
                            ]),
                            None,
                            kytten.VerticalLayout([
                                kytten.Label("Build"),
                                kytten.Label("farm"),
                            ]),
                            None,
                            kytten.VerticalLayout([
                                kytten.Label("Build"),
                                kytten.Label("mine"),
                            ]),
                            None,
                        ]),
                        None,None,None,
                        kytten.HorizontalLayout([
                            kytten.Button("Cancel", on_click=self.cancel),
                            None, None,
                            kytten.Button("Confirm", on_click=self.cancel),
                        ]),
	                ]),
	            ),
	        batch=self.batchnode.batch,window=director.window,theme=KYTTEN_THEME,movable=False)
      
      
        #print self.dialog._get_controls()
        
#        self.okbutton = self.dialog._get_controls()[5][0]
      
            
        self.mineralsLabel = Label( str(self.minerals),
            font_size=12,
            bold=True,
            x=director.get_window_size()[0]/2-220,
            y=director.get_window_size()[1]/2-70)
            
        self.peopleLabel = Label( str(self.population),
            font_size=12,
            bold=True,
            x=director.get_window_size()[0]/2+75,
            y=director.get_window_size()[1]/2-70)
            
        self.organicsLabel = Label( str(self.organics),
            font_size=12,
            bold=True,
            x=director.get_window_size()[0]/2-75,
            y=director.get_window_size()[1]/2-70)
            
        self.add( self.mineralsLabel )
        self.add( self.peopleLabel )
        self.add( self.organicsLabel )
                        
                        
    def on_dummy(self,a):
        print a
        
            
    def launch(self):
        if self.toPlanet != None:
            print "Building confirmed"
        self.kill()
            
            
    def cancel(self):
        print "Building aborted"
        self.kill()
    

    def on_enter(self):
        super(BuildDialog, self).on_enter() 
        #pause game
        #self.galaxy.running = False
        
        
    def on_exit(self):
        super(BuildDialog, self).on_exit() 
        self.dialog.teardown()
        self.batchnode.on_exit()
        # unpause game
        #self.galaxy.running = True
        
    def on_key_press( self, k , m ):
        if k == key.ESCAPE:
            self.cancel()
            return pyglet.event.EVENT_HANDLED # this prevents ESC to pop this scene (default behaviour)

    def _distance(self,start,end):
        return sqrt( (end[0] - start[0])**2 + (end[1] - start[1])**2 )
         
